
import authDict from './authDict'
export type AuthKey = keyof typeof authDict
class Auth {
    list: String[] = []
    authMap: Map<AuthKey, boolean> = new Map()
    init (data: String[]) {
        this.list = data;
    }
    // 按钮链接权限
    check (value: string): boolean {
        const isExist = this.list.includes(value)
        return isExist;
    }

    // 按钮弹窗权限
    btnAuth(arr: string[]) {
        for (const item of arr) {
          if (!this.list.includes(item)) return false
        }
        return true
      }
    isBtn(key: AuthKey){
        if (!this.authMap.has(key)) {
            this.authMap.set(key, this.btnAuth(authDict[key]))
          }
          return this.authMap.get(key)!
    }
    
    hasPermission = (el:any, binding:any) => {
        // 判断用户是否拥有权限
        if (this.isBtn(binding.value)) {
          // 如果拥有，显示按钮
          el.style.display = '';
        } else {
          // 如果不拥有，隐藏按钮
          el.style.display = 'none';
        }
      };
    //  自定义标签
    initDirective = () => {
        const that = this
        return {
            mounted(el:any, binding:any) {
                that.hasPermission(el, binding);
            },
            updated(el:any, binding:any) {
                that.hasPermission(el, binding);
            }  
        }
    }
    
}
const auth = new Auth()

export default auth
